Array.prototype.binarySearch = function (item) {

    // 非递归版本
    let left = 0,
        right = this.length - 1
    while (left <= right) {  // 这里一定是小于等于，否则只有一个元素时无法找到
        let midIndex = Math.floor((left + right) / 2)
        let midElement = this[midIndex]
        if (item > midElement) {
            left = midIndex + 1
        } else if (item < midElement) {
            right = midIndex - 1
        } else {
            return midIndex
        }
    }
    return -1
}

const res = [1, 2, 3, 4, 5, 8].binarySearch(3)
console.log(res)